import request from "@/utils/request";
import { APP_ID, APP_SECRET } from "@/enums/CacheEnum";
import type { AuthorizeCode, TokenData,UserInfo,LoginData,UserManageList,createUserData,deleteUserData,resetUserData,setInvalidData,updateUserData} from "../auth/userList"

class AuthAPI {

	/**
	 * 用户登陆
	 */
	static getUesrLogin(data: LoginData) {
		const formData = new FormData();
		formData.append("account", data.account);
		formData.append("pwd", data.pwd);
		return request<any, UserInfo>({
			url: "/user/login",
			method: "post",
			data: formData,
			headers: {
			},
		});
	}
	//获取用户列表
	static getUesrList(AuthorizeCode: string, data: any) {
		// console.log('data.invalid', data.invalid);
		let formobj={
			page:data.page,
			page_size:data.page_size,
			search:data.search,
			// invalid:'',
			invalid:!isNaN(data.invalid) ? data.invalid : undefined,
		}
		if (!isNaN(data.invalid)) {
			formobj.invalid = data.invalid;
		}else{
			delete formobj.invalid
		}
		return request<any>({
		  url: `/user/page_list`,
		  method: "get",
		  params:formobj,
		  headers: {
			token: AuthorizeCode,
		  },
		});
	  }
	/**
	 * 创建用户
	 */
	static createUser(AuthorizeCode: string,data: any) {
		// console.log('data',data.user_type);
		const formData = new FormData();
		formData.append("username", data.username);
		formData.append("user_type",data.user_type);
		formData.append("account", data.account);
		formData.append("memo", data.memo);
		formData.append("pwd", data.pwd);

		return request<any, UserInfo>({
			url: "/user/create",
			method: "post",
			data: formData,
			headers: {
				token: AuthorizeCode,
			  },
		});
	}
	/**
	 * 删除
	 */
	static deleteUser(AuthorizeCode: string, data: string) {
		// console.log(data);
		const formData = new FormData();
		formData.append("user_id", data);
		return request<any, UserInfo>({
		  url: "/user/set_delete",
		  method: "post",
		  data: formData,
		  headers: {
			token: AuthorizeCode,
			"Content-Type":"multipart/form-data"
		  },
		});
	  }
	/**
	 * 重置密码
	 */
	static resetUser(AuthorizeCode: string, data: string) {
		// console.log(data);
		const formData = new FormData();
		formData.append("user_id", data);
		
		return request<any, UserInfo>({
			url: "/user/reset_password",
			method: "post",
			data: formData,
			headers: {
			token: AuthorizeCode,
			},
		});
		}
	/**
	 * 获取用户信息
	 */
	static getUser(AuthorizeCode: string, data: any) {
		let formobj={
			user_id:data,
		}
		return request<any, UserInfo>({
		  url: "/user/get_info",
		  method: "get",
		//   data: formData,
		  params:formobj,
		  headers: {
			token: AuthorizeCode,
		  },
		});
	  }
	/**
	 * 设置用户无效
	 */
	static setInvalid(AuthorizeCode: string,data: setInvalidData) {
		// console.log(data);
		const formData = new FormData();
		formData.append("user_id", data.user_id);
		formData.append("invalid", data.invalid);
		return request<any, UserInfo>({
		  url: "/user/set_invalid",
		  method: "post",
		  data: formData,
		  headers: {
			token: AuthorizeCode,
		  },
		});
	  }
	/**
	 * 编辑保存
	 */
	static updateUser(AuthorizeCode: string,id:string,data:any) {
		const formData = new FormData();
		formData.append("username", data.username);
		formData.append("user_type",data.user_type);
		formData.append("account", data.account);
		formData.append("memo", data.memo);
		formData.append("user_id", id);

		return request<any, UserInfo>({
			url: "/user/edit",
			method: "post",
			data: formData,
			headers: {
				token: AuthorizeCode,
			  },
		});
	}
}

export default AuthAPI;
